package library.and.xps.com.step.adapter;

import android.content.Context;
import android.content.Intent;
import android.graphics.Color;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.bumptech.glide.load.model.GlideUrl;
import com.bumptech.glide.load.model.LazyHeaders;
import com.jude.utils.JUtils;
import com.zhy.autolayout.AutoLinearLayout;

import jp.wasabeef.glide.transformations.CropCircleTransformation;
import library.and.xps.com.step.R;
import library.and.xps.com.step.SPConstant;
import library.and.xps.com.step.URLConstants;
import library.and.xps.com.step.m.model.TeamChallengeRes;
import library.and.xps.com.step.view.activity.PersonalChallengeDetailActivity;

//个人挑战
public class PersonChallengeAdapter extends AbstractRecyclerAdapter<TeamChallengeRes.ListBean> {

    public PersonChallengeAdapter(Context context, RecyclerView recyclerView) {
        super(context, recyclerView);
    }

    @Override
    protected RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType, boolean isItem) {
        View v = LayoutInflater.from(parent.getContext()).inflate(
                R.layout.team_challenge_item, parent, false);
        return new MyViewHolder(v);
    }

    @Override
    protected void onBindViewHolder(RecyclerView.ViewHolder holder, int position, boolean isItem) {
        MyViewHolder myViewHolder = (MyViewHolder) holder;
        final TeamChallengeRes.ListBean data = getItem(position);
        JUtils.Log("onBindViewHolder",data.toString());
        setupCircleImg(context, myViewHolder.iv_bechallenger_header, data.getBico());
        setupCircleImg(context, myViewHolder.iv_challenger_header, data.getCico());
        myViewHolder.tv_read_time.setText(data.getReadingtimes());
        myViewHolder.tv_bechallenger_exc.setText(data.getBsteps() + "");
        myViewHolder.tv_challenger_exc.setText(data.getSteps() + "");
        myViewHolder.tv_bechallenger_name.setText(data.getBname());
        myViewHolder.tv_challenger_name.setText(data.getCname());
        myViewHolder.al_item_challenger.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
              if(data.getState()==0||data.getState()==1||data.getState()==2){
                  Intent i = new Intent(context, PersonalChallengeDetailActivity.class);
                  i.putExtra("pkid", data.getUserpkid());
                  i.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                  context.startActivity(i);
              }
            }
        });

        initChallengerState(myViewHolder, data.getState());
    }

    private void initChallengerState(MyViewHolder myViewHolder, int state) {
        switch (state) {
            case -1://已取消
                myViewHolder.al_state_bar.setBackgroundColor(Color.parseColor("#f4f4f4"));
                myViewHolder.tv_state.setText("已取消");
                myViewHolder.tv_state.setTextColor(Color.parseColor("#7e7e7e"));
                break;
            case 0://等待响应
                myViewHolder.al_state_bar.setBackgroundColor(Color.parseColor("#FF489DFB"));
                myViewHolder.tv_state.setText("等待响应");
                myViewHolder.tv_state.setTextColor(Color.WHITE);
                break;
            case 1://即将开始
                myViewHolder.al_state_bar.setBackgroundColor(Color.parseColor("#FF489DFB"));
                myViewHolder.tv_state.setText("即将开始");
                myViewHolder.tv_state.setTextColor(Color.WHITE);
                break;
            case 2://进行中
                myViewHolder.al_state_bar.setBackgroundColor(Color.parseColor("#f67070"));
                myViewHolder.tv_state.setText("进行中");
                myViewHolder.tv_state.setTextColor(Color.WHITE);
                break;
            case 3://拒绝
                myViewHolder.al_state_bar.setBackgroundColor(Color.parseColor("#FFD700"));
                myViewHolder.tv_state.setText("拒绝");
                myViewHolder.tv_state.setTextColor(Color.WHITE);
                break;
            case 4://结束
                myViewHolder.al_state_bar.setBackgroundColor(Color.parseColor("#f4f4f4"));
                myViewHolder.tv_state.setText("结束");
                myViewHolder.tv_state.setTextColor(Color.parseColor("#7e7e7e"));
                break;
        }
    }

    public void setupCircleImg(Context context, ImageView imageView, String url) {
        if("null".equals(url)||"NULL".equals(url)|| TextUtils.isEmpty(url))return;

        String imgUrl = URLConstants.FILE.FileDownload + url;

        GlideUrl glideUrl = new GlideUrl(imgUrl, new LazyHeaders.Builder()
                .addHeader("token", JUtils.getSharedPreference().getString(SPConstant.USER_TOKEN, ""))
                .build());

        Glide.with(context).load(glideUrl)
                .placeholder(R.drawable.default_header_male)
                .bitmapTransform(new CropCircleTransformation(context))
                .into(imageView);
    }

    private static class MyViewHolder extends RecyclerView.ViewHolder {
        TextView tv_challenger_name, tv_bechallenger_name, tv_challenger_exc, tv_bechallenger_exc, tv_read_time, tv_state;
        ImageView iv_bechallenger_header, iv_challenger_header;
        AutoLinearLayout al_state_bar, al_item_challenger;

        MyViewHolder(View itemView) {
            super(itemView);
            tv_challenger_name = (TextView) itemView.findViewById(R.id.tv_challenger_name);
            tv_bechallenger_name = (TextView) itemView.findViewById(R.id.tv_bechallenger_name);
            tv_challenger_exc = (TextView) itemView.findViewById(R.id.tv_challenger_exc);
            tv_bechallenger_exc = (TextView) itemView.findViewById(R.id.tv_bechallenger_exc);
            tv_read_time = (TextView) itemView.findViewById(R.id.tv_read_time);
            iv_bechallenger_header = (ImageView) itemView.findViewById(R.id.iv_bechallenger_header);
            iv_challenger_header = (ImageView) itemView.findViewById(R.id.iv_challenger_header);
            al_state_bar = (AutoLinearLayout) itemView.findViewById(R.id.al_cancel_pk);
            tv_state = (TextView) itemView.findViewById(R.id.tv_cancel_pk);
            al_item_challenger = (AutoLinearLayout) itemView.findViewById(R.id.al_item_challenger);
        }

    }
}
